<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018-08-02
 * Time: 下午 4:25
 */
namespace app\api\controller;
use think\Controller;
use think\Db;
class Api extends Controller{
    //会员信息
    public function getMemberInfo(){
        $member_id = input('param.member_id');
        if(!$member_id){
            ajaxReturn(-1,'member_id不能为空');
        }
        $member = new Member();
        $memberInfo = $member->getMemberInfo($member_id);
        ajaxReturn(1,'查询成功',$memberInfo);
    }
    //修改会员信息
    public function saveMemberInfo(){
        $param = input('param');
        if(!$param['member_id']){
            ajaxReturn(-1,'member_id不能为空');
        }
        if($param['mobile']){
            //手机号存在则验证验证码
            if(!$param['code']){
                ajaxReturn(-1,'code手机验证码不能为空');
            }
            $send = new Send();
            $send_status = $send->checkSendCode($param['mobile'],$param['code']);
            if(!$send_status){
                ajaxReturn(-1,'验证码不正确');
            }
        }
        if($param['custom']){
            $param['custom'] = json_encode($param['custom']);
        }
        $data = array(
            'name'=>$param['name']?:'',
            'sex'=>$param['sex']?:'',
            'birthday'=>$param['birthday']?:'',
            'is_calendar'=>$param['is_calendar']?:'',
            'wx_no'=>$param['wx_no']?:'',
            'mobile'=>$param['mobile']?:'',
            'email'=>$param['email']?:'',
            'company_name'=>$param['company_name']?:'',
            'job'=>$param['job']?:'',
            'work'=>$param['work']?:'',
            'city'=>$param['city']?:'',
            'address'=>$param['address']?:'',
            'home'=>$param['home']?:'',
            'address_home'=>$param['address_home']?:'',
            'taste'=>$param['taste']?:'',
            'brief'=>$param['brief']?:'',
            'custom'=>$param['custom']?:'',
            'headimg'=>$param['headimg']?:''
        );
        $member = new Member();
        $save_status = $member->saveMemberInfo($param['member_id'],$data);
        if($save_status){
            ajaxReturn(1,'修改成功');
        }else{
            ajaxReturn(-1,'修改失败');
        }
    }
    //发送消息通知
    public function sendMsg(){
        $param = input('param');
        if(!$param['notice_id']){
            ajaxReturn(-1,'通知人ID不能为空');
        }
        if(!$param['send_id']){
            ajaxReturn(-1,'发送者不能为空');
        }
        if(!$param['content']){
            ajaxReturn(-1,'发送内容不能为空');
        }
        if(!$param['type']){
            ajaxReturn(-1,'消息类型不能为空');
        }
        $data = array(
            'notice_id'=>$param['notice_id'],
            'send_id'=>$param['send_id'],
            'content'=>$param['content'],
            'type'=>$param['type'],
            'time'=>time(),
        );
        $member  = new Member();
        $msg_status = $member->sendMsg($data);
        if($msg_status){
            ajaxReturn(1,'发送成功');
        }else{
            ajaxReturn(-1,'发送失败');
        }
    }
    //添加好友
    public function addFriends(){
        $param = input('param');
        if(!$param['fid']){
            ajaxReturn(-1,'好友ID不能为空');
        }
        if(!$param['member_id']){
            ajaxReturn(-1,'当前用户ID不能为空');
        }
        $data = array(
            'member_id1'=>$param['member_id'],
            'member_id2'=>$param['fid']
        );
        $member = new Member();
        $friends_status = $member->addFriends($data);
        if($friends_status){
            ajaxReturn(1,'添加好友成功');
        }else{
            ajaxReturn(-1,'添加好友失败');
        }
    }
    //好友群列表
    public function getFriendsList(){
        $param = input('param');
        if(!$param['page']){
            ajaxReturn(-1,'当前也不能为空');
        }
        if(!$param['count']){
            ajaxReturn(-1,'每页展示个数');
        }
        if(!$param['member_id']){
            ajaxReturn(-1,'会员ID不能为空');
        }
        $member = new Member();
        $friendsList = $member->getFriendsList($param['page'],$param['count'],$param['member_id']);
        $qunList = $member->getMemberQun($param['page'],$param['count'],$param['member_id']);
        //$data = array_merge($friendsList,$qunList);
        foreach($friendsList as $k=>$v){
            if($v['is_zd1'] == 1){
                $arr[]['member_id'] = $v['member_id'];
                $arr[]['name'] = $v['name'];
                $arr[]['headimg'] = $v['headimg'];
                $arr[]['is_zd'] = $v['is_zd1'];
                $arr[]['time'] = $v['time1'];
                $arr[]['is_type'] = 1;//1为会员
            }else {
                if ($v['is_zd2'] == 1) {
                    $arr[]['member_id'] = $v['member_id'];
                    $arr[]['name'] = $v['name'];
                    $arr[]['headimg'] = $v['headimg'];
                    $arr[]['is_zd'] = $v['is_zd2'];
                    $arr[]['time'] = $v['time2'];
                    $arr[]['is_type'] = 1;//1为会员
                }else{
                    $acc[]['member_id'] = $v['member_id'];
                    $acc[]['name'] = $v['name'];
                    $acc[]['headimg'] = $v['headimg'];
                    $acc[]['is_zd'] = $v['is_zd1'];
                    $acc[]['time'] = $v['time1'];
                    $acc[]['is_type'] = 1;//1为会员
                }
            }

        }
        foreach($qunList as $key=>$val){
            if($val['is_zd'] == 1){
                $arr[]['qun_id'] = $val['qun_id'];
                $arr[]['name'] = $val['name'];
                $arr[]['headimg'] = $val['img'];
                $arr[]['is_zd'] = $val['is_zd'];
                $arr[]['time'] = $val['time'];
                $arr[]['is_type'] = 2;//2为群
            }else{
                $acc[]['qun_id'] = $val['qun_id'];
                $acc[]['name'] = $val['name'];
                $acc[]['headimg'] = $val['img'];
                $acc[]['is_zd'] = $val['is_zd'];
                $acc[]['time'] = $val['time'];
                $acc[]['is_type'] = 2;//2为群
                unset($qunList[$key]);
            }

        }
        //排序置顶列表
        $zdList = array_map(create_function('$n', 'return $n["time"];'), $arr);
        array_multisort($zdList, SORT_DESC, $arr );
        $data['zd'] = $arr;
        $data['pt'] = $acc;
        ajaxReturn(1,'查询成功',$data);
    }
    //会员资源
    public function getMemberSource(){
        $param = input('param');
        if(!$param['page']){
            ajaxReturn(-1,'当前也不能为空');
        }
        if(!$param['count']){
            ajaxReturn(-1,'每页展示个数');
        }
        if(!$param['member_id']){
            ajaxReturn(-1,'会员ID不能为空');
        }
        if(!$param['type']){
            ajaxReturn(-1,'资源类型');
        }
        $source = new Source();
        $memberSourceList = $source->getMemberSource($param['page'],$param['count'],$param['member_id'],$param['type']);
        ajaxReturn(1,'查询成功',$memberSourceList);
    }
    //查询备注
    public function getMemberRemarks(){
        $param = input('param');
        if(!$param['member_id']){
            ajaxReturn(-1,'当前会员ID不能为空');
        }
        if(!$param['friends_id']){
            ajaxReturn(-1,'好友ID不能为空');
        }
        $member = new Member();
        $remarks = $member->getMemberRemarks($param['friends_id'],$param['member_id']);
        ajaxReturn(1,'查询成功',$remarks);
    }
    //修改好友备注
    public function saveMemberRemarks(){
        $param = input('param');
        if(!$param['remarks']){
            ajaxReturn(-1,'备注不能为空');
        }
        if(!$param['member_id']){
            ajaxReturn(-1,'当前会员ID不能为空');
        }
        if(!$param['friends_id']){
            ajaxReturn(-1,'好友ID不能为空');
        }
        $data = json_encode($param['remarks']);
        $member = new Member();
        $save_status = $member->saveMemberRemarks($param['friends_id'],$param['member_id'],$param['remarks']);
        if($save_status == false){
            ajaxReturn(-1,'修改失败');
        }else{
            ajaxReturn(1,'修改成功');
        }
    }
    //置顶会员
    public function setMemberIsZd(){
        $param = input('param');
        if(!$param['member_id']){
            ajaxReturn(-1,'当前会员ID不能为空');
        }
        if(!$param['friends_id']){
            ajaxReturn(-1,'好友ID不能为空');
        }
        if(!$param['is_zd']){
            ajaxReturn(-1,'置顶状态不能为空');
        }
        $member = new Member();
        $zd_status = $member->setMemberIsZd($param['friends_id'],$param['member_id'],$param['is_zd']);
        if($zd_status == false){
            ajaxReturn(-1,'置顶失败');
        }else{
            ajaxReturn(1,'置顶成功');
        }
    }
    //群信息
    public function getQunInfo(){
        $param = input('param');
        if(!$param['qun_id']){
            ajaxReturn(-1,'群id不能为空');
        }

    }
    //置顶群
    public function setQunIsZd(){
        $param = input('param');
        if(!$param['member_id']){
            ajaxReturn(-1,'当前会员ID不能为空');
        }
        if(!$param['qun_id']){
            ajaxReturn(-1,'群ID不能为空');
        }
        if(!$param['is_zd']){
            ajaxReturn(-1,'置顶状态不能为空');
        }
        $qun = new Qun();
        $zd_status = $qun->setQunIsZd($param['member_id'],$param['qun_id'],$param['is_zd']);
        if($zd_status == false){
            ajaxReturn(-1,'操作失败');
        }else{
            ajaxReturn(1,'操作成功');
        }
    }
    //群资源
    public function getQunSource(){
        $param = input('param');
        if(!$param['qun_id']){
            ajaxReturn(-1,'群ID不能为空');
        }
        if(!$param['page']){
            ajaxReturn(-1,'当前页不能为空');
        }
        if(!$param['count']){
            ajaxReturn(-1,'每页个数');
        }
        if(!$param['member_id']){
            ajaxReturn(-1,'当前会员ID');
        }
        $source = new Source();
        $data = $source->qunSourceList($param['qun_id'],$param['page'],$param['count']);
        foreach($data as $k=>$v){
            $data[$k]['is_collect'] = $source->checkCollectSource($v['source_id'],$param['member_id']);
        }
        ajaxReturn(1,'查询成功',$data);
    }
    //群成员
    public function getQunMember(){
        $param = input('param');
        if(!$param['qun_id']){
            ajaxReturn(-1,'群ID不能为空');
        }
        if(!$param['page']){
            ajaxReturn(-1,'当前页不能为空');
        }
        if(!$param['count']){
            ajaxReturn(-1,'每页个数');
        }
        $qun = new Qun();
        $data = $qun->qunMemberList($param['qun_id'],$param['page'],$param['count']);
        ajaxReturn(1,'查询成功',$data);
    }
    //修改群信息
    public function saveQunInfo(){
        $param = input('param');
        if(!$param['qun_id']){
            ajaxReturn(-1,'群ID不能为空');
        }
        if(!$param['name']){
            ajaxReturn(-1,'群名称不能为空');
        }
        if(!$param['notice']){
            ajaxReturn(-1,'群公告不能为空');
        }
        if(!$param['img']){
            ajaxReturn(-1,'群头像不能为空');
        }
        $qun = new Qun();
        $save_status = $qun->saveQunInfo($param['qun_id'],$param['name'],$param['notice'],$param['img']);
        if($save_status == false){
            ajaxReturn(-1,'操作失败');
        }else{
            ajaxReturn(1,'操作成功');
        }
    }
    //添加群标签
    public function addQunSig(){
        $param = input('param');
        if(!$param['qun_id']){
            ajaxReturn(-1,'群ID不能为空');
        }
        if(!$param['sig']){
            ajaxReturn(-1,'群ID不能为空');
        }
        $sigInfo = implode(',',$param['sig']);
        for($i=0;$i<count($sigInfo);$i++){
            $arr[$i]['name'] = $sigInfo[$i];
            $arr[$i]['time'] = time();
            $arr[$i]['qun_id'] = $param['qun_id'];
        }
        $qun = new Qun();
        $add_status = $qun->addQunSig($param['qun_id'],$arr);
        if($add_status == false){
            ajaxReturn(-1,'操作失败');
        }else{
            ajaxReturn(1,'操作成功');
        }
    }
    //群标签列表
    public function qunSigList(){
        $param = input('param');
        if(!$param['qun_id']){
            ajaxReturn(-1,'群ID不能为空');
        }
        if(!$param['page']){
            ajaxReturn(-1,'当前页不能为空');
        }
        if(!$param['count']){
            ajaxReturn(-1,'每页个数');
        }
        $qun = new Qun();
        $data = $qun->qunSigList($param['qun_id'],$param['page'],$param['count']);
        ajaxReturn(1,'查询成功',$data);
    }
    //设置群资源标签
    public function setQunSourceSig(){
        $param = input('param');
        if(!$param['qun_id']){
            ajaxReturn(-1,'群ID不能为空');
        }
        if(!$param['source_id']){
            ajaxReturn(-1,'资源ID不能为空');
        }
        if(!$param['sig_id']){
            ajaxReturn(-1,'资源ID不能为空');
        }
        $qun = new Qun();
        $set_status = $qun->setQunSourceSig($param['qun_id'],$param['source_id'],$param['sig_id']);
        if($set_status == false){
            ajaxReturn(-1,'操作失败');
        }else{
            ajaxReturn(1,'操作成功');
        }
    }
    //设置管理员
    public function setQunMaster(){
        $param = input('param');
        if(!$param['qun_id']){
            ajaxReturn(-1,'群ID不能为空');
        }
        if(!$param['member_id']){
            ajaxReturn(-1,'当前ID不能为空');
        }
        if(!$param['qun_member_id']){
            ajaxReturn(-1,'被设置人ID不能为空');
        }
        if(!$param['is_head']){
            ajaxReturn(-1,'被设置人ID不能为空');
        }
        //检查会员再群里面的角色
        $member = new Member();
        $role_status = $member->getMemberRole($param['member_id'],$param['qun_id']);
        if($role_status !=1){
            ajaxReturn(-1,'只有群主才能设置管理员');
        }
        Db::startTrans();
        try{
            $arr = explode(',',$param['qun_member_id']);
            if(in_array($param['member'],$arr)){
                ajaxReturn(-1,'群主不能设置为管理员');
            }
            for($i=0;$i<count($arr);$i++){
                $member->setQunMaster($param['qun_id'],$arr[$i],$param['is_head']);
            }
            // 提交事务
            Db::commit();
            ajaxReturn(1,'操作成功');
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            ajaxReturn(-1,'操作失败');
        }
    }
    //成员管理
    public function setQunMember(){
        $param = input('param');
        if(!$param['qun_id']){
            ajaxReturn(-1,'群ID不能为空');
        }
        if(!$param['member_id']){
            ajaxReturn(-1,'当前操作会员ID不能为空');
        }
        if(!$param['qun_member_id']){
            ajaxReturn(-1,'删除群成员ID不能为空');
        }
        //检查会员再群里面的角色
        $member = new Member();
        $role_status = $member->getMemberRole($param['member_id'],$param['qun_id']);
        if($role_status !=1 || $role_status !=2){
            ajaxReturn(-1,'只有群主或者管理员才能管理群会员');
        }
        Db::startTrans();
        try{
            $arr = explode(',',$param['qun_member_id']);
            if(in_array($param['member'],$arr)){
                ajaxReturn(-1,'自己不能剔除自己');
            }
            for($i=0;$i<count($arr);$i++){
                $role_status_qun = $member->getMemberRole($param['member_id'],$arr[$i]);
                if($role_status_qun == 1){
                    ajaxReturn(-1,'不能剔除群主');
                    break;
                }elseif($role_status_qun == 2 && $role_status == 2){
                    ajaxReturn(-1,'管理员不能剔除管理员');
                    break;
                }
                $member->deleteQunMember($param['qun_id'],$arr[$i]);
            }
            // 提交事务
            Db::commit();
            ajaxReturn(1,'操作成功');
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            ajaxReturn(-1,'操作失败');
        }
    }
    //对群可见的资源
    public function getQunMemberSource(){
        $param = input('param');
        if(!$param['qun_id']){
            ajaxReturn(-1,'群ID不能为空');
        }
        if(!$param['member_id']){
            ajaxReturn(-1,'当前操作会员ID不能为空');
        }
        $source = new Source();
        $data= $source->getQunMemberSource($param['qun_id'],$param['member_id']);
        ajaxReturn(1,'查询成功',$data);
    }
    //退出删除转让群
    public function outQun(){
        $param = input('param');
        if(!$param['qun_id']){
            ajaxReturn(-1,'群ID不能为空');
        }
        if(!$param['member_id']){
            ajaxReturn(-1,'当前操作会员ID不能为空');
        }
        if(!$param['is_type']){
            ajaxReturn(-1,'状态值不能为空');
        }
        if(!$param['qun_member_id']){
            ajaxReturn(-1,'转让人ID不能为空');
        }
        $qun = new Qun();
        $member = new Member();
        //检查会员再群里面的角色
        $role_status = $member->getMemberRole($param['member_id'],$param['qun_id']);
        if($param['is_type'] == 1){
            //退出群
            if($role_status == 1){
                //群主 直接解散群
                $qun_status = $qun->outQun($param['qun_id']);
            }else{
                //群成员直接退出群
                $qun_status = $qun->tuiQun($param['qun_id'],$param['member_id']);
            }

        }else{
            //转让群
            if($role_status == 1){
                $qun_status = $qun->zhuanQun($param['qun_id'],$param['member_id'],$param['qun_member_id']);
            }
        }
        if($qun_status){
            ajaxReturn(1,'操作成功');
        }else{
            ajaxReturn(-1,'操作失败');
        }
    }
    //对他可见的资源
    public function friendSeeMySource(){
        $param = input('param');
        if(!$param['friend_id']){
            ajaxReturn(-1,'群ID不能为空');
        }
        if(!$param['member_id']){
            ajaxReturn(-1,'当前操作会员ID不能为空');
        }
        $source = new Source();
        $data = $source->friendSeeMySource($param['member_id'],$param['friend_id']);
        ajaxReturn(1,'查询成功',$data);
    }
    //检测两个会员之前是不是好友
    public function getMemberMyFriend(){
        $param = input('param');
        if(!$param['member_id1']){
            ajaxReturn(-1,'会员ID不能为空');
        }
        if(!$param['member_id2']){
            ajaxReturn(-1,'会员ID不能为空');
        }
        $member = new Member();
        $member_status = $member->getMemberMyFriend($param['member_id1'],$param['member_id2']);
        if($member_status){
            ajaxReturn(1,'是好友');
        }else{
            ajaxReturn(-1,'不是是好友');
        }
    }
    //创建群
    public function addQun(){
        $param = input('param');
        if(!$param['name']){
            ajaxReturn(-1,'群名称不能为空');
        }
        if(!$param['notice']){
            ajaxReturn(-1,'群公告不能为空');
        }
        if(!$param['img']){
            ajaxReturn(-1,'群头像不能为空');
        }
        if(!$param['member_id']){
            ajaxReturn(-1,'创建者ID不能为空');
        }
        $qun = new Qun();
        Db::startTrans();
        try{
            $qun_id = $qun->addQun($param['name'],$param['notice'],$param['img'],$param['member_id']);
            //添加群成员
            $data = array(
                'qun_id'=>$qun_id,
                'member_id'=>$param['member_id'],
                'is_master'=>1,
            );
            $qun->addQunMember($data);
            // 提交事务
            Db::commit();
            ajaxReturn(1,'创建成功');
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            ajaxReturn(-1,'创建失败');
        }
    }
    //加入群
    public function joinQun(){
        $param = input('param');
        if(!$param['qun_id']){
            ajaxReturn(-1,'群id 不能为空！');
        }
        if(!$param['member_id']){
            ajaxReturn(-1,'会员ID不能为空');
        }
        $qun = new Qun();
        //添加群成员
        $data = array(
            'qun_id'=>$param['qun_id'],
            'member_id'=>$param['member_id']
        );
        $qun_status = $qun->addQunMember($data);
        if($qun_status){
            ajaxReturn(1,'加入成功');
        }else{
            ajaxReturn(-1,'加入失败');
        }
    }
    //上传群资源
    public function uploadQunSource(){
        $param = input('param');
        if(!$param['qun_id']){
            ajaxReturn(-1,'群id 不能为空！');
        }
        if(!$param['member_id']){
            ajaxReturn(-1,'会员ID不能为空');
        }
        if(!$param['source_id']){
            ajaxReturn(-1,'资源ID不能为空');
        }
        $data = array(
            'qun_id'=>$param['qun_id'],
            'member_id'=>$param['member_id'],
            'source_id'=>$param['source_id'],
            'time'=>time()
        );
        $member = new Member();
        $member_status= $member->memberUploadQunSource($data);
        if($member_status){
            ajaxReturn(1,'上传成功');
        }else{
            ajaxReturn(-1,'上传失败');
        }
    }
    //添加收藏资源
    public function getMemberCollect(){
        $param = input('param');
        if(!$param['member_id']){
            ajaxReturn(-1,'会员ID不能为空');
        }
        if(!$param['source_id']){
            ajaxReturn(-1,'资源ID不能为空');
        }
        $data = array(
            'member_id'=>$param['member_id'],
            'source_id'=>$param['source_id'],
            'time'=>time()
        );
        $member = new Member();
        //检查是不是收藏自己的资源
        $status = checkMyCollectMySource($param['member_id'],$param['source_id']);
        if($status == 1){
            ajaxReturn(-1,'自己不能收藏自己的资源');
        }
        $member_status= $member->memberCollect($data);
        if($member_status){
            ajaxReturn(1,'收藏成功');
        }else{
            ajaxReturn(-1,'收藏失败');
        }
    }
    //收藏列表
    public function getMemberCollectList(){
        $param = input('param');
        if(!$param['member_id']){
            ajaxReturn(-1,'会员ID不能为空');
        }
        if(!$param['page']){
            ajaxReturn(-1,'当前页不能为空');
        }
        if(!$param['count']){
            ajaxReturn(-1,'每页数量不能为空');
        }
        if(!$param['source_type']){
            ajaxReturn(-1,'资源类型');
        }
        $member = new Member();
        $data= $member->memberCollectList($param['member_id'],$param['page'],$param['count'],$param['source_type']);
        ajaxReturn(1,'查询成功',$data);
    }
    //取消收藏
    public function deleteMemberCollect(){
        $param = input('param');
        if(!$param['member_id']){
            ajaxReturn(-1,'会员ID不能为空');
        }
        if(!$param['source_id']){
            ajaxReturn(-1,'资源ID');
        }
        $member = new Member();
        $member_status= $member->deleteMemberCollect($param['member_id'],$param['source_id']);
        if($member_status){
            ajaxReturn(1,'取消成功');
        }else{
            ajaxReturn(-1,'取消失败');
        }
    }
    //我的资源列表
    public function mineSourceList(){
        $param = input('param');
        if(!$param['member_id']){
            ajaxReturn(-1,'会员ID不能为空');
        }
        if(!$param['page']){
            ajaxReturn(-1,'当前页');
        }
        if(!$param['count']){
            ajaxReturn(-1,'每页个数');
        }
        if(!$param['source_type']){
            ajaxReturn(-1,'资源类型');
        }
        $source = new Source();

        //自己查看自己的资源列表
        $data = $source->getMemberSource($param['member_id'],$param['page'],$param['count'],$param['source_type']);

        ajaxReturn(1,'查询成功',$data);
    }
    //别人查看自己的资源信息
    public function hisSourceList(){
        $param = input('param');
        if(!$param['member_id']){
            ajaxReturn(-1,'会员ID不能为空');
        }
        if(!$param['friend_id']){
            ajaxReturn(-1,'谁查看的ID');
        }
        $source = new Source();
        $data = $source->friendSeeMySource($param['member_id'],$param['friend_id']);
        foreach($data as $k=>$v){
            $data[$k]['is_collect'] = $source->checkCollectSource($v['source_id'],$param['friend_id']);
        }
        ajaxReturn(1,'查询成功',$data);
    }
    //添加个人名片
    public function addMyCard(){
        $param = input('param');
        if(!$param['name']){
            ajaxReturn(-1,'会员姓名');
        }
        if(!$param['sex']){
            ajaxReturn(-1,'性别');
        }
        if(!$param['company_name']){
            ajaxReturn(-1,'公司名称');
        }
        if(!$param['job']){
            ajaxReturn(-1,'公司职位');
        }
        if(!$param['address']){
            ajaxReturn(-1,'公司地址');
        }
        if(!$param['mobile']){
            ajaxReturn(-1,'手机号');
        }
        if(!$param['custom']){
            ajaxReturn(-1,'自定义属性');
        }
        if(!$param['member_id']){
            ajaxReturn(-1,'会员ID');
        }
        $data = array(
            'name'=>$param['name'],
            'sex'=>$param['sex'],
            'company_name'=>$param['company_name'],
            'job'=>$param['job'],
            'address'=>$param['address'],
            'mobile'=>$param['mobile'],
            'custom'=>json_encode($param['custom']),
            'member_id'=>$param['member_id']
        );
        $member = new Member();
        $member_status = $member->addMyCard($data);
        if($member_status){
            ajaxReturn(1,'添加成功');
        }else{
            ajaxReturn(-1,'添加失败');
        }
    }
    //查询会员名片
    public function getMemberCard(){
        $param = input('param');
        if(!$param['member_id']){
            ajaxReturn(-1,'会员ID');
        }
        $member = new Member();
        $data = $member->getMemberCard($param['member_id']);
        ajaxReturn(1,'查询成功',$data);
    }
    //创建资源
    public function addMemberSource(){
        $param = input('param');
        if(!$param['detail']){
            ajaxReturn(-1,'资源详情不能为空');
        }
        if(!$param['type']){
            ajaxReturn(-1,'资源类型');
        }
        if(!$param['is_type']){
            ajaxReturn(-1,'权限类别');
        }
        if(!$param['member_id']){
            ajaxReturn(-1,'性别');
        }
        if($param['is_type'] !=1 ){
            $data['power'] = $param['member_power']?:'';
            $qun_power = $param['qun_power'];
        }
        $data['img'] = json_decode($param['img']?:'');
        $data['detail'] = $param['detail'];
        $data['type'] = $param['type'];
        $data['is_type'] = $param['is_type'];
        $data['member_id'] = $param['member_id'];
        $data['addtime'] = date('Y-m-d H:i:s');
        $source = new Source();
        $member = new Member();
        Db::startTrans();
        try{
            //添加资源
            $source_id  = $source->addSource($data);
            if($qun_power){
                $qun_arr = explode(',',$qun_power);
                for($i=0;$i<count($qun_arr);$i++){
                    $arr['member_id'] = $param['member'];
                    $arr['source_id'] = $source_id;
                    $arr['qun_id'] = $qun_arr[$i];
                    $member->memberUploadQunSource($arr);
                }
            }
            // 提交事务
            Db::commit();
            ajaxReturn(1,'创建成功');
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            ajaxReturn(-1,'创建失败');
        }

    }
    //修改资源
    public function saveMemberSource(){
        $param = input('param');
        if(!$param['detail']){
            ajaxReturn(-1,'资源详情不能为空');
        }
        if(!$param['type']){
            ajaxReturn(-1,'资源类型');
        }
        if(!$param['is_type']){
            ajaxReturn(-1,'权限类别');
        }
        if(!$param['source_id']){
            ajaxReturn(-1,'资源ID');
        }
        if(!$param['member_id']){
            ajaxReturn(-1,'会员ID');
        }
        if($param['is_type'] !=1 ){
            $data['power'] = $param['member_power']?:'';
            $qun_power = $param['qun_power'];
        }
        $data['img'] = json_decode($param['img']?:'');
        $data['detail'] = $param['detail'];
        $data['type'] = $param['type'];
        $data['is_type'] = $param['is_type'];
        $source = new Source();
        $member = new Member();
        Db::startTrans();
        try{
            //添加资源
            $source->saveSource($param['source_id'],$data);
            if($qun_power){
                $qun_arr = explode(',',$qun_power);
                for($i=0;$i<count($qun_arr);$i++){
                    $member->memberDeleteQunSource($param['source_id'],$param['member_id'],$qun_arr[$i]);
                    $arr['member_id'] = $param['member_id'];
                    $arr['source_id'] = $param['source_id'];
                    $arr['qun_id'] = $qun_arr[$i];
                    $member->memberUploadQunSource($arr);
                }
            }
            // 提交事务
            Db::commit();
            ajaxReturn(1,'创建成功');
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            ajaxReturn(-1,'创建失败');
        }

    }
    //会员取消上传群资源
    public function memberDeleteUploadSource(){
        $param = input('param');
        if(!$param['member_id']){
            ajaxReturn(-1,'会员ID');
        }
        if(!$param['qun_id']){
            ajaxReturn(-1,'群ID');
        }
        if(!$param['source_id']){
            ajaxReturn(-1,'资源ID');
        }
        $member = new Member();
        $member_status = $member->memberDeleteQunSource($param['source_id'],$param['member_id'],$param['qun_id']);
        if($member_status){
            ajaxReturn(1,'取消成功');
        }else{
            ajaxReturn(-1,'取消失败');
        }
    }
    //意见反馈
    public function  feedBack(){
        $param = input('param');
        if(!$param['content']){
            ajaxReturn(-1,'资源详情不能为空');
        }
        if(!$param['type']){
            ajaxReturn(-1,'手机号或邮箱');
        }
        if(!$param['member_id']){
            ajaxReturn(-1,'会员ID');
        }
        $data = array(
            'content'=>$param['content'],
            'type'=>$param['type'],
            'member_id'=>$param['member_id'],
            'time'=>time()
        );
        $member = new Member();
        $member_status = $member->feedBack($data);
        if($member_status){
            ajaxReturn(1,'提交成功');
        }else{
            ajaxReturn(-1,'提交失败');
        }
    }
    //举报
    public function memberReport(){
        $param = input('param');
        if(!$param['content']){
            ajaxReturn(-1,'举报原因');
        }
        if(!$param['type']){
            ajaxReturn(-1,'手机号或邮箱');
        }
        if(!$param['member_id']){
            ajaxReturn(-1,'举报人ID');
        }
        $data = array(
            'content'=>$param['content'],
            'type'=>$param['type'],
            'member_id'=>$param['member_id'],
            'img'=>json_encode($param['img']?:''),
            'add_time'=>time()
        );
        $member = new Member();
        $member_status = $member->memberReport($data);
        if($member_status){
            ajaxReturn(1,'举报成功');
        }else{
            ajaxReturn(-1,'举报失败');
        }

    }
    //搜索资源
    public function searchSource(){
        $param = input('param');
        if(!$param['member_id']){
            ajaxReturn(-1,'会员ID');
        }
        if(!$param['keyword']){
            ajaxReturn(-1,'关键字');
        }
        //所有好友信息
        $member = new Member();
        $member_data = $member->getFriendsListInfo($param['member_id'],$param['keyword']);
        //取出所有的memberID
        foreach($member_data as $k=>$v){
            $member_id[] = $v['member_id'];
        }
        //加入的所有群
        $qun_data = $member->getMemberQunInfo($param['member_id']);
        //取出所有的qunID
        foreach($qun_data as $k=>$v){
            $qun_id[] = $v['qun_id'];
        }
        //查询所有的群资源
        $source = new Source();
        $qun_id_str = implode(',',$qun_id);
        $qun_source_data = $source->qunSourceListAll($qun_id_str,$param['keyword']);
        //查询所有好友自己可见的资源
        $friend_id_str  = implode(',',$member_id);
        $member_source_data = $source->friendSeeMySourceAll($param['member_id'],$friend_id_str,$param['keyword']);
        //把所有群资源跟会员资源合并
        foreach($qun_source_data as $k=>$v){
            $allData[] = $v;
        }
        foreach($member_source_data as $k=>$v){
            $allData[] = $v;
        }
        $data = array(
            'member'=>$member_data,
            'qun'=>$allData
        );
        ajaxReturn(1,'查询成功',$data);
    }
    //资源动态
    public function sourceDynamicFriend(){
        $param = input('param');
        if(!$param['member_id']){
            ajaxReturn(-1,'会员ID');
        }
        if(!$param['type']){
            ajaxReturn(-1,'资源类型');
        }
        //所有好友信息
        $member = new Member();
        $member_data = $member->getAllFriendsList($param['member_id']);
        //取出所有的memberID
        foreach($member_data as $k=>$v){
            $member_id[] = $v['member_id'];
        }
        $source = new Source();
        //查询所有好友自己可见的资源
        $friend_id_str  = implode(',',$member_id);
        $member_source_data = $source->friendSeeMySourceAllType($param['member_id'],$friend_id_str,$param['type']);
        foreach($member_source_data as $k=>$v){
            $member_source_data[$k]['is_collect'] = $source->checkCollectSource($v['source_id'],$param['member_id']);
        }
        ajaxReturn(1,'查询成功',$member_source_data);
    }
    //资源动态
    public function sourceDynamicQun(){
        $param = input('param');
        if(!$param['member_id']){
            ajaxReturn(-1,'会员ID');
        }
        if(!$param['type']){
            ajaxReturn(-1,'资源类型');
        }
        $member = new Member();
        //加入的所有群
        $qun_data = $member->getMemberQunInfo($param['member_id']);
        //取出所有的qunID
        foreach($qun_data as $k=>$v){
            $qun_id[] = $v['qun_id'];
        }
        //查询所有的群资源
        $source = new Source();
        $qun_id_str = implode(',',$qun_id);
        $qun_source_data = $source->qunSourceListAllType($qun_id_str,$param['type']);
        foreach($qun_source_data as $k=>$v){
            $qun_source_data[$k]['is_collect'] = $source->checkCollectSource($v['source_id'],$param['member_id']);
        }
        ajaxReturn(1,'查询成功',$qun_source_data);
    }
    //生产添加好友的二维码地址
    public function getMemberQrcode(){

        $float = $this->checkAssignToken();
        if($float!=0){
            $token = $float;
        }else{
            $result = getAssignToken();
            $content['time'] = time()+$result['expires_in'];//到期时间
            $content['token'] = $result['access_token'];//token
            $data['content'] = json_encode($content);
            $res = Db::name('config')->where('config_id',2)->update($data);
            if($res === false){
                ajaxReturn(-1,'获取token失败');
            }
            $token = $content['token'];
        }
        ajaxReturn(1,'成功',$token);
    }

    //检测assigntoken是否到期
    private function checkAssignToken(){
        $float = 1;
        $data = Db::name('config')->where('config_id',2)->find();
        if(empty($data)){
            $float = 0;
        }
        if(!$data['content']){
            $float = 0;
        }
        $arr = json_decode($data['content'],true);
        $now = time();
        if($now>$arr['time']){
            $float = 0;
        }
        return $arr['token'];
    }

}